/*
 * DataSenderManager.cpp
 *
 *  Created on: Mar 16, 2009
 *      Author: Andrzej Olchawa
 *      mailto: andrzejolchawa@gmail.com
 *      http://www.hardtechnology.org
 *      This is a part of the Armazd project, which is covered by
 *      GNU GPL v2 license.
 */

#include "DataSenderManager.h"

DataSenderManager::DataSenderManager()
{
	setDataSender(AgentConfiguration::getDestinationsConfigPaht());
}

DataSenderManager::~DataSenderManager()
{
	if (sender)
	{
		delete sender;
	}
}

DataSender *DataSenderManager::getDataSender()
{
	return sender;
}

void DataSenderManager::setDataSender(const string fileName)
{
	HTProperties properties;
	try
	{
		properties.loadProperties(fileName);

		sender = new DataSender();

		for (int i=0; i<properties.getNumberOfElements(); i++)
		{
			string name, host;
			int port;

			try
			{
				name = properties[i].key;
				AgentUtils::extractDestinationsInfoFromConfigLine(host,
						port, properties[i].value, i+1);
			}
			catch (AgentException &e)
			{
				e.printException();
			}
			catch (HTException &aoe)
			{
				aoe.printException();
			}

			sender->addDestination(new DestinationHost(name, host, port));
		}
	}
	catch (HTException &aoe)
	{
		aoe.printException();
	}
}
